package com.dc.util;

import com.dc.common.DateUtil;
import com.dc.exceptions.ActivationCodeException;

import java.util.Date;

public class ReCodeUtil {

    private static final String key = "CSFL-";

    /**
     * 解密
     *
     * @param desValue
     * @param keyId
     */
    public static String dnCode(String desValue, String keyId) {
        EncryptUtil encryptUtil = EncryptUtil.getInstance();
        try {
            String value = encryptUtil.DESdecode(desValue, key + keyId);
            return value;
        } catch (Exception exception) {
            throw new ActivationCodeException("激活失败，请重新获取激活码");
        }
    }

    public static String getVersion(String value) {
        try {
            return value.substring(0, 1); //T 试用版本  O:正式版本
        } catch (Exception exception) {
            throw new ActivationCodeException("激活失败，请重新获取激活码");
        }

    }

    public static long getDays(String value) {
        try {
            String time = value.substring(1, 9);
            Date date = DateUtil.parseStrToDate(time, DateUtil.DATE_FORMAT_YYYYMMDD);
            if (date.getTime() < new Date().getTime()) {
                //加密时间大于当前时间 已过期
                throw new ActivationCodeException("激活码已过期");
            }
            try {
                Long days = DateUtil.getDistanceDays(date, new Date());
                return days;
            } catch (Exception exception) {
                throw new ActivationCodeException("激活码激活失败");
            }
        } catch (Exception exception) {
            throw new ActivationCodeException("激活码激活失败");
        }
    }
}
